package com.car.dao.impl;

import com.car.dao.CarDao;
import com.car.domain.Car;
import com.car.utils.JDBCUtils;

import java.util.List;

/**
 * 汽车Dao的实现类
 */
public class CarDaoImpl implements CarDao {
    /**
     * 新增
     *
     * @param car 实体类
     * @return 影像记录数
     */
    @Override
    public int add(Car car)throws Exception{
        String sql = "inset into t_car(name,brand,price,type,color,car_num,employee_id)"+
                "value(?,?,?,?,?,?,?,?)";
        return JDBCUtils.update(sql,car.getName(),car.getBrand(),car.getPrice(),car.getType(),car.getColor(),
                car.getCarNum(),car.getEmployeeId());
    }


    /**
     * 删除
     *
     * @param id 实体类
     * @return 影响记录数
     */
    @Override
    public int delete(int id)throws  Exception{
        String sql = "delete from t_car where id =?";
        return JDBCUtils.update(sql,id);
    }

    /**
     * 修改
     *
     * @param car 实体类
     * @return 影响记录数
     */
    @Override
    public int update(Car car) throws Exception{
        String sql = "update t_car set name=?,brand=?,price=?,type=?,color=?,car_num=?,"+
                "status=?,employee_id=?, where=?";
        return JDBCUtils.update(sql,car.getName(),car.getBrand(),car.getPrice(),car.getType(),car.getColor(),
                car.getCarNum(),car.getEmployeeId(),car.getStatus(),car.getId());
    }

    /**
     * 通过主键查询
     *
     * @param id 主键id
     * @return 实体类
     */
    @Override
    public Car queryById(int id) throws Exception{
        String sql ="select * from t_car where id = ?";
        return JDBCUtils.query(sql,Car.class,id);
    }

    /**
     * 查询所有记录
     *
     * @return 所有实体类
     */
    @Override
    public List<Car> queryAll() throws Exception {
        String sql = "select * from t_car";
        return JDBCUtils.queryList(sql, Car.class);
    }

    @Override
    public Car findByIdAndStatusEq0(int carId) throws Exception {
        String sql = "select * from t_car where status = 0 and id =?";
        return JDBCUtils.query(sql, Car.class,carId);
    }
}
